package com.ruoyi.forum.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.forum.domain.ForumReview;
import com.ruoyi.forum.domain.ForumReviewUpvote;
import com.ruoyi.forum.service.IForumReviewService;
import com.ruoyi.forum.service.IForumReviewUpvoteService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论-点赞Controller
 * 
 * @author ruoyi
 * @date 2024-02-13
 */
@RestController
@RequestMapping("/forum/reviewUpvote")
public class ForumReviewUpvoteController extends BaseController
{
    @Autowired
    private IForumReviewUpvoteService forumReviewUpvoteService;

    @Autowired
    private IForumReviewService  forumReviewService;

//    @Autowired
//    private IForumReviewService  forumReviewService;

    /**
     * 查询评论-点赞列表
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumReviewUpvote forumReviewUpvote)
    {
        startPage();
        List<ForumReviewUpvote> list = forumReviewUpvoteService.selectForumReviewUpvoteList(forumReviewUpvote);
        return getDataTable(list);
    }

    /**
     * 导出评论-点赞列表
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:export')")
    @Log(title = "评论-点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumReviewUpvote forumReviewUpvote)
    {
        List<ForumReviewUpvote> list = forumReviewUpvoteService.selectForumReviewUpvoteList(forumReviewUpvote);
        ExcelUtil<ForumReviewUpvote> util = new ExcelUtil<ForumReviewUpvote>(ForumReviewUpvote.class);
        util.exportExcel(response, list, "评论-点赞数据");
    }

    /**
     * 获取评论-点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumReviewUpvoteService.selectForumReviewUpvoteById(id));
    }

    /**
     * 新增评论-点赞
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:add')")
    @Log(title = "评论-点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumReviewUpvote forumReviewUpvote)
    {
        forumReviewUpvote.setCreateBy(getUsername());
        return toAjax(forumReviewUpvoteService.insertForumReviewUpvote(forumReviewUpvote));
    }

    /**
     * 获取评论列表-根据用户id和帖子id
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:likeList')")
    @Log(title = "评论-获取评论列表-根据用户id和帖子id", businessType = BusinessType.INSERT)
    @GetMapping("/likeList")
    public AjaxResult getLikeListForUserPost(@RequestParam("userId") Long userId, @RequestParam("postId") Long postId)
    {
        return success(forumReviewUpvoteService.getLikeListForUserPost(userId, postId));
    }

    /**
     * 请求评论-点赞（如果已点赞取消点赞，未点赞就点赞）
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:like')")
    @Log(title = "评论-点赞like", businessType = BusinessType.INSERT)
    @PostMapping("/like")
    @Transactional
    public AjaxResult like(@RequestBody ForumReviewUpvote forumReviewUpvote)
    {
        List<ForumReviewUpvote> forumReviewUpvotes = forumReviewUpvoteService.selectForumReviewUpvoteList(forumReviewUpvote);
        ForumReview review = forumReviewService.selectForumReviewById(forumReviewUpvote.getReviewId());
        if(forumReviewUpvotes!=null&&forumReviewUpvotes.size()>0){
            Long[] longs = new Long[forumReviewUpvotes.size()];
            for(int i=0;i<forumReviewUpvotes.size();i++){
                longs[i]=forumReviewUpvotes.get(i).getId();
            }
            if(forumReviewUpvoteService.deleteForumReviewUpvoteByIds(longs)>0){
                review.setUpvoteNumber(review.getUpvoteNumber()-1);
                forumReviewService.updateForumReview(review);
                return new AjaxResult(200,"取消点赞成功");
            }else {
                return new AjaxResult(200,"取消点赞失败");
            }
        }else {
            forumReviewUpvote.setCreateBy(getUsername());
            if(forumReviewUpvoteService.insertForumReviewUpvote(forumReviewUpvote)>0){
                review.setUpvoteNumber(review.getUpvoteNumber()+1);
                forumReviewService.updateForumReview(review);
                return new AjaxResult(200,"点赞成功");
            }else {
                return new AjaxResult(200,"点赞失败");
            }
        }
    }

    /**
     * 修改评论-点赞
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:edit')")
    @Log(title = "评论-点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumReviewUpvote forumReviewUpvote)
    {
        forumReviewUpvote.setUpdateBy(getUsername());
        return toAjax(forumReviewUpvoteService.updateForumReviewUpvote(forumReviewUpvote));
    }

    /**
     * 删除评论-点赞
     */
    @PreAuthorize("@ss.hasPermi('forum:reviewUpvote:remove')")
    @Log(title = "评论-点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumReviewUpvoteService.deleteForumReviewUpvoteByIds(ids));
    }
}
